/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.depaul.se.courseregistration.sessionbean;
import edu.depaul.se.courseregistration.jpa.courses.*;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Group2
 */

@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class DBAdminEJB {

    @PersistenceContext(unitName="CourseRegistrationModulePU")
    private EntityManager em;
    
    //DBAdminSession buisness APIs
    //Create Course
    //@Override
    public Course createCourse(Subject subject,int courseId, String courseTitle, String coursePrerequisite, 
                                           String courseBrief, int quarterNumber,String termTitle, String termYear ) {
        
        
        //Build new course skeleton
        Course newCourse = new Course();
        newCourse.setCourseNumber(courseId);
        newCourse.setCourseTitle(courseTitle);
        newCourse.setCoursePrerequisite(coursePrerequisite);
        newCourse.setCourseBrief(courseBrief);
        
        //Build new term
        Term newTerm = new Term();
        newTerm.setQuarterNumber(quarterNumber);
        newTerm.setTermTitle(termTitle);
        newTerm.setYear(termYear);
        
        //Add term to new course and tie them to an exsiting subject
        newCourse.setTerm(newTerm);
        newCourse.setSubjectID(subject);
        
        em.persist(newCourse);
        return newCourse;
    }
    
    //@Override
    public void removeCourse(int courseNumber)
    {
        Course course = em.createNamedQuery(Course.COURSE_QUERY_BY_ID, Course.class).
        setParameter("courseNumber", courseNumber).getSingleResult();
        em.remove(course);
    }
    
    //@Override
    public void updateCourse(Course courseToUpdate,String courseTitle, String coursePrerequisite, String courseBrief, 
               int quarterNumber,String termTitle, String termYear )
    {
        //Build new term
        Term newTerm = null;
        if(quarterNumber!=0 ||termTitle!=null || termYear!=null)
        {
            newTerm = new Term();
            if(quarterNumber!=0)
                newTerm.setQuarterNumber(quarterNumber);
            if(termTitle!=null)
                newTerm.setTermTitle(termTitle);
            if(termYear!=null)
                newTerm.setYear(termYear);
        }
        if(newTerm !=null)
           courseToUpdate.setTerm(newTerm);
                    
       //Make the changes
       if(courseTitle!=null)
           courseToUpdate.setCourseTitle(courseTitle);
       if(coursePrerequisite!=null)
           courseToUpdate.setCoursePrerequisite(coursePrerequisite);
       if(courseBrief!=null)
           courseToUpdate.setCourseBrief(courseBrief);
       
       em.merge(courseToUpdate);
    }
    
    //Create, Remove, Update Section
    //@Override
    public Section createSection(Course course, int sectionId,int sectionPrice, boolean isOpen,String instructorName,
                                 String sectionDates, String meetingHours, String buildingName, String roomNumber) {
        
        //Create new section
       Section newSection = new Section();
       newSection.setCourseNumber(course);
       newSection.setSectionPrice(sectionPrice);
       newSection.setSectionID(sectionId);
       newSection.setSectionStaus(isOpen);
       newSection.setInstructorName(instructorName);
       newSection.setectionDates(sectionDates);
       newSection.setMeetingHours(meetingHours);
       newSection.setBuildingName(buildingName);
       newSection.setRoomNumber(roomNumber);
        em.persist(newSection);
        return newSection;
    }
    
   //@Override
    public void removeSection(int courseSectionID)
    {
        Section courseSection = em.createNamedQuery(Section.SECTION_QUERY_BY_ID, Section.class).
        setParameter("sectionId", courseSectionID).getSingleResult();
        em.remove(courseSection);
    }
    
    //@Override
    public void updateSection(Section sectionToUpdate, int sectionPrice, boolean isOpen,String instructorName,
            String sectionDates, String meetingHours, String buildingName, String roomNumber)
    {
        //Make the changes if not null
       if(sectionPrice!=0) 
          sectionToUpdate.setSectionPrice(sectionPrice);
       if(isOpen==true || isOpen==false)
           sectionToUpdate.setSectionStaus(isOpen);
       if(instructorName!=null)
           sectionToUpdate.setInstructorName(instructorName);
       if(sectionDates!=null)
           sectionToUpdate.setectionDates(sectionDates);
       if(meetingHours!=null)
           sectionToUpdate.setMeetingHours(meetingHours);
       if(buildingName!=null)
           sectionToUpdate.setBuildingName(buildingName);
       if(roomNumber!=null)
           sectionToUpdate.setRoomNumber(roomNumber);
       //merge the changes
       em.merge(sectionToUpdate);
    }
    
    //Create, Remove, Update Subject
    //@Override
    public Subject createSubject(College college, String subjectCode, String subjectDescription, int subjectid) {
        //Build new subject
        Subject newSubject = new Subject();
        newSubject.setSubjectCode(subjectCode);
        newSubject.setSubjectID(subjectid);
        newSubject.setSubjectDescription(subjectDescription);
        newSubject.setCollegeId(college);
        em.persist(newSubject);
        return newSubject;
    }
    //@Override
    public void removeSubject(int subjectID)
    {
        Subject subject = em.createNamedQuery(Subject.SUBJECT_QUERY_BY_ID, Subject.class).
        setParameter("subjectID", subjectID).getSingleResult();
        em.remove(subject);
    }
    //@Override 
    public void updateSubject(Subject subjectToUpdate,String subjectCode, String subjectDescription )
    {
        //Make the changes
        if(subjectCode !=null)
            subjectToUpdate.setSubjectCode(subjectCode);
        if(subjectDescription!=null)
            subjectToUpdate.setSubjectDescription(subjectDescription); 
        em.merge(subjectToUpdate);
    }
    
    //Create, Remove, Update College
   //@Override
    public College createCollege(int collegeID, String collegeName, String collegeShortName) {
        //Build new college
       College newCollege = new College();
       newCollege.setCollegeId(collegeID); //lmarco fix webservice failing.
       newCollege.setCollegeName(collegeName);
       newCollege.setCollegeShortName(collegeShortName);
       newCollege.setSubjectList(null);
        em.persist(newCollege);
        return newCollege;
    }
    
    //@Override
    public void removeCollege(int collegeId)
    {
        College college = em.createNamedQuery(College.COLLEGE_QUERY_BY_ID, College.class).
        setParameter("collegeId", collegeId).getSingleResult();
        em.remove(college);
    }
    //@Override    
    public void updateCollege(College collegeToUpdate, String collegeName, String collegeShortName)
    {
       //Make the changes
       if(collegeName!=null) 
           collegeToUpdate.setCollegeName(collegeName);
       if(collegeShortName!=null)
           collegeToUpdate.setCollegeShortName(collegeShortName);
       
        em.merge(collegeToUpdate);
    }
}